package com.example.administrator.pms.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.administrator.pms.bean.MySQLiteHelper;
import com.example.administrator.pms.bean.Picture;

import java.util.ArrayList;
import java.util.List;

public class PictureCollectDao {

    public MySQLiteHelper helper;

    public PictureCollectDao(Context context){
        helper = new MySQLiteHelper(context);
    }

    //插入图片
    public void insertPicture(Picture picture){
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("insert into picture(_url,_picid,_name,_label)" + "values(?,?,?,?)",
                new String[]{picture.getUrl(),null,picture.getName(),picture.getLabel()});
        db.close();
    }

    //删除图片
    public void deletePicture(String picid){
        SQLiteDatabase db = helper.getReadableDatabase();
        db.execSQL("delete from picture where _picid = ?",new Object[]{picid});
        db.close();
    }

    //查询图片
    public List<Picture> findPicture(){

        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor c = db.rawQuery("select * from picture",null);
        List<Picture> list = new ArrayList<Picture>();

        while(c.moveToNext()){
            Picture picture = new Picture();
            picture.setUrl(c.getString(c.getColumnIndex("_url")));
            picture.setName(c.getString(c.getColumnIndex("_name")));
            picture.setLabel(c.getString((c.getColumnIndex("_label"))));
            list.add(picture);
        }

        c.close();
        db.close();
        return list;
    }

}
